package com.framewiki.example.web.controller;

import com.cdkjframework.core.controller.WebUiController;
import com.cdkjframework.entity.PageEntity;
import com.cdkjframework.exceptions.GlobalRuntimeException;
import com.cdkjframework.util.tool.CopyUtils;
import com.framewiki.example.dto.RmsUserDto;
import com.framewiki.example.service.RmsUserService;
import com.framewiki.example.vo.RmsUserVo;
import io.swagger.v3.oas.annotations.media.SchemaProperty;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ProjectName: bms-manger
 * @Package: com.lesmarthome.web.rms.controller
 * @ClassName: UserController
 * @Author: yiMaoPing
 * @Version: 1.0
 * @Description: 用户信息与前端的交互
 **/
@RequestMapping(value = "/rms/user")
@RestController
@Tag(name = "用户")
@RequiredArgsConstructor
public class RmsUserController extends WebUiController {

  /**
   * 用户服务
   */
  private final RmsUserService rmsUserServiceImpl;

  /**
   * 新增账户信息
   *
   * @param userVo 用户登录信息
   */
  @ResponseBody
  @PostMapping(value = "/insertRmsUser")
  @SchemaProperty(name = "新增账户信息")
  public void insertRmsUser(@RequestBody RmsUserVo userVo) {
    RmsUserDto userDto = CopyUtils.copyNoNullProperties(userVo, RmsUserDto.class);
    rmsUserServiceImpl.addRmsUser(userDto);
  }

  /**
   * 删除账户信息
   *
   * @param userVo 用户登录信息
   */
  @ResponseBody
  @PostMapping(value = "/deleteRmsUser")
  @SchemaProperty(name = "删除账户信息")
  public void deleteRmsUser(@RequestBody RmsUserVo userVo) {
    RmsUserDto userDto = CopyUtils.copyNoNullProperties(userVo, RmsUserDto.class);
    rmsUserServiceImpl.deleteRmsUser(userDto);
  }

  /**
   * 修改账户信息
   *
   * @param userVo 用户登录信息
   */
  @ResponseBody
  @PostMapping(value = "/modifyRmsUser")
  @SchemaProperty(name = "修改账户信息")
  public void modifyRmsUser(@RequestBody RmsUserVo userVo) {
    RmsUserDto userDto = CopyUtils.copyProperties(userVo, RmsUserDto.class);
    rmsUserServiceImpl.modifyRmsUser(userDto);
  }

  /**
   * 分页展示用户登陆信息的数据集
   *
   * @param userVo 用户登录信息
   */
  @ResponseBody
  @PostMapping(value = "/listRmsUserPage")
  @SchemaProperty(name = "分页展示用户登陆信息的数据集")
  public PageEntity<RmsUserVo> listRmsUserPage(@RequestBody RmsUserVo userVo) {
    RmsUserDto userDto = CopyUtils.copyNoNullProperties(userVo, RmsUserDto.class);
    PageEntity<RmsUserDto> page = rmsUserServiceImpl.listRmsUserPage(userDto);

    // 返回结果
    return PageEntity.build(page.getPageIndex(), page.getTotal(), CopyUtils.copyProperties(page.getData(), RmsUserVo.class));
  }
}
